local w,h = 250,100
local ui = class.panel:builder 
{
    normal_image = [[ui\挑战面板背景框.tga]],
    x = 550,
    y = 760,
    w = w,
    h = h,
    alpha = 0.99,
    show_flag = false,
}
ui:set_level(99)
local w,h = 64,64
ui.buttons = {}
local keys = {'Q','W','E'}
for a=1,3 do
    local button = ui:add_sp_button((a-1)*(w+6)+20,8,w,h)
    button:set_highlight([[UI\Widgets\Console\Human\CommandButton\human-activebutton.blp]])
    button.hotkey = keys[a]
    button.ani_flag = true
    button.title = ui:add_text('金币挑战',button.x-5,button.h,w+10,30,8,5)
    button.index = a
    ui.buttons[a] = button
end


function ui:set_data(data)
    for a,button in ipairs(self.buttons) do
        local skill = data[a]
        button.skill = skill
        if skill then
            button.title:set_text(skill:get_title())
            button:show()
        else
            button:hide()
        end
    end
    self:show()
end

function ui:update()
    for a,button in ipairs(self.buttons) do
        button:refresh(button.skill)
    end
end

function ui:on_button_mouse_enter(button)
    local skill = button.skill
    if skill then
        local tip = skill:get_tip()
        self:tooltip(skill.title or skill.name,tip,'top')
    end
end

function ui:on_button_clicked(button)
    local index = button.index
    if index then
        ac.sync('玩家-点击挑战',index)
    end
end


ac.hardware:event '界面绘制'(function()
    ui:update()
end)

ac.game:event '玩家-设置挑战内容'(function(_,player,data)
    if player:is_self() then
        print(data)
        if data then
            ui:set_data(data)
        else
            ui:hide()
        end
    end
end)